/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.shop.rest;

import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.logging.aop.log.Log;
import link.chengguo.orangemall.modules.aop.ForbidSubmit;
import link.chengguo.orangemall.modules.shop.domain.OrangeSystemStore;
import link.chengguo.orangemall.modules.shop.domain.OrangeSystemStoreStaff;
import link.chengguo.orangemall.modules.shop.service.OrangeSystemStoreService;
import link.chengguo.orangemall.modules.shop.service.OrangeSystemStoreStaffService;
import link.chengguo.orangemall.modules.shop.service.dto.OrangeSystemStoreStaffDto;
import link.chengguo.orangemall.modules.shop.service.dto.OrangeSystemStoreStaffQueryCriteria;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
* @author chengguo
* @date 2020-03-22
*/
@Api(tags = "门店店员管理")
@RestController
@RequestMapping("/api/orangeSystemStoreStaff")
public class SystemStoreStaffController {

    private final OrangeSystemStoreStaffService orangeSystemStoreStaffService;
    private final OrangeSystemStoreService orangeSystemStoreService;

    private final IGenerator generator;

    public SystemStoreStaffController(OrangeSystemStoreService orangeSystemStoreService, OrangeSystemStoreStaffService orangeSystemStoreStaffService, IGenerator generator) {
        this.orangeSystemStoreService = orangeSystemStoreService;
        this.orangeSystemStoreStaffService = orangeSystemStoreStaffService;
        this.generator = generator;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('OrangeSystemStoreStaff:list')")
    public void download(HttpServletResponse response, OrangeSystemStoreStaffQueryCriteria criteria) throws IOException {
        orangeSystemStoreStaffService.download(generator.convert(orangeSystemStoreStaffService.queryAll(criteria), OrangeSystemStoreStaffDto.class), response);
    }

    @GetMapping
    @Log("查询门店店员")
    @ApiOperation("查询门店店员")
    @PreAuthorize("@el.check('OrangeSystemStoreStaff:list')")
    public ResponseEntity<Object> getOrangeSystemStoreStaffs(OrangeSystemStoreStaffQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orangeSystemStoreStaffService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增门店店员")
    @ApiOperation("新增门店店员")
    @PreAuthorize("@el.check('OrangeSystemStoreStaff:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OrangeSystemStoreStaff resources){
        OrangeSystemStore systemStore = orangeSystemStoreService.getOne(Wrappers.<OrangeSystemStore>lambdaQuery()
                .eq(OrangeSystemStore::getId,resources.getStoreId()));
        resources.setStoreName(systemStore.getName());
        return new ResponseEntity<>(orangeSystemStoreStaffService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改门店店员")
    @ApiOperation("修改门店店员")
    @PreAuthorize("@el.check('OrangeSystemStoreStaff:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OrangeSystemStoreStaff resources){
        OrangeSystemStore systemStore = orangeSystemStoreService.getOne(Wrappers.<OrangeSystemStore>lambdaQuery()
                .eq(OrangeSystemStore::getId,resources.getStoreId()));
        resources.setStoreName(systemStore.getName());
        orangeSystemStoreStaffService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除门店店员")
    @ApiOperation("删除门店店员")
    @PreAuthorize("@el.check('OrangeSystemStoreStaff:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        orangeSystemStoreStaffService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
